function scritturaDati(data){
	var dati="<TABLE><TR><TH>lower</TH><TH>upper</TH><TH>status</TH></TR>";
	var i;
	$('.table').show();
	$('.button').show();
//	alert(data.resultRanges.length);
	for(i=0;i<data.resultRanges.length;i++){
		dati+="<TR class='"+data.resultRanges[i].status+" clickable'><TD name='lower'>"+data.resultRanges[i].lower+"</TD><TD name='upper'>"+data.resultRanges[i].upper+"</TD><TD>"+data.resultRanges[i].status+"</TD></TR>";
	}
	dati+="</TABLE>";
	$('.table').html(dati);
	$('.clickable').click(this,
			function(data){
//				console.log(data);
				var lower=$(data.currentTarget >'TD [name="lower"]').val();//SISTEMARE********************************
				console.log(lower);
			}
	);
//	$('tr:even').css('background-color','#bbbbff');
};

function invioDati(userLower,userUpper){
	//inviare i dati al server per la manipolazione
//	alert("dati inviati"+userLower+"-"+userUpper);
	$.getJSON("./dummy.json",{lower:userLower,upper:userUpper},scritturaDati);
};

function init(){
	$('.box').hide();
	$('.box').val("");
	$('.table').hide();
	$('.table').html("");
	$('.button').hide();
}
function eanSelectPopulate(){
	$.getJSON("./dummyEan.json",function(data){
		var i;
		for(i=0;i<data.length;i++){
			$('select#prefix').append("<option>"+data[i].ean+"</option>");
//			alert(e.ean);
		}
	});
}
function retrieveTable(ean){
	init();
	if(ean=="dummy"){
		dummy();
	}else if(ean=="--Select ean prefix--"){
		//niente
	}else{
		dummy();
	}
	$.getJSON("./dummy.json",scritturaDati);
};
function dummy(){
	$('.box').show();
	
};

$(document).ready(
		function(){
			init();
			eanSelectPopulate();
			$('.button').click(
				function(){
					alert("Bottone Premuto");
				}
			);
			$('.box').keypress(
					function(){
//						alert("o yeah!");
						invioDati($('input[name="lower"]').val(),$('input[name="upper"]').val());
					}
			);
			$('select').change(
					function(){
						var opt=$('select option:selected').val();
						retrieveTable(opt);
					}
			);
		}
);